package org.tony.utils;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.tony.db.DbUtil;
import org.tony.entity.SubmitData;

/**
 * 接收oracle提交的数据，解析并存储
 * @author tony
 *
 */
public class ParseSubmitInfo extends HttpServlet {

	private static final long serialVersionUID = 7249596085648990289L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		
		
		String value = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+toStringHex(request.getParameter("valueChange")) ;
		PrintWriter out = response.getWriter();
		out.println("TRUE");
		out.flush();
		out.close();
		
		SubmitData sd = null;
		try {
			System.out.println(value);
			sd = PareseSubmitDataUtils.parseData(value);
			String sql = PareseSubmitDataUtils.generateSql(sd);
			DbUtil.execute(sql, JdbcHelper.system) ;
			DbUtil.execute(sql, JdbcHelper.backup) ;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	
	public static String toStringHex(String s) {
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(
						i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		try {
			s = new String(baKeyword, "GBK");//UTF-16le:Not
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return s;
	}

}
